<?php

namespace app\admin\controller;

use app\common\business\BusBase;
use app\common\business\User;
use app\common\lib\Show;
use app\common\lib\sms\AliSms;
use think\facade\View;
use app\admin\business\AdminUser;
use app\admin\validate\AdminUser as Validate;

class Login extends AdminBase
{
    public function initialize()
    {
        if ($this->isLogin()) {
            return $this->redirect(url("index/index"));
        }
    }

    public function index()
    {
        return View::fetch();
    }

    public function md5()
    {
        // admin   admin
        halt(session(config("admin.session_admin")));
        echo md5("admin_singwa_abc");
    }

    public function check()
    {
        if (!$this->request->isPost()) {
            return Show::fail(config('status.error'), "请求方式错误");
        }
        $username = $this->request->param('username');
        $password = $this->request->param('password');
        $captcha = $this->request->param('captcha');
        $data = [
            'username' => $username,
            'password' => $password,
            'captcha' => $captcha,
            'ip' => $this->request->ip()
        ];
        $validate = new Validate();
        if (!$validate->check($data)) {
            return Show::fail(config('status.error'), $validate->getError());
        }
        try {
            $ret = AdminUser::getInstance()->login($data);
            if (is_array($ret)) {
                return Show::fail($ret);
            }
        } catch (\Exception $e) {
            return Show::fail(config("status.error"), '内部异常，登录失败');
        }
        return Show::failOrSuccess($ret, config('status.error'), null, '登录失败');
    }
}